<template>
	<view class="box1 login">
		<view class="login_head u-flex u-row-center u-flex-col">
			<image src="../../static/img/login.png"></image>
			<view class="p">
				<text class="login_font color u-font-xl text-bold">LOCUS MUSIC</text>
			</view>
		</view>
		<view class="login_body u-flex u-row-center u-flex-col">
			<view class="w9" v-show="swith_flag==true">
				<u-field v-model="phone" label="手机号:" placeholder="请填写手机号" icon="phone-fill" icon-color="#4058D1">
				</u-field>
				<u-field v-model="pwd" label="密码:" placeholder="请填写密码" password icon="lock-fill" icon-color="#4058D1">
				</u-field>
			</view>

			<view class="w9" v-show="swith_flag==false">
				<u-field v-model="phone" label="手机号:" placeholder="请填写手机号" icon="phone-fill" icon-color="#4058D1">
				</u-field>
				<u-field v-model="code" label="验证码:" placeholder="请填写验证码" icon="hourglass-half-fill"
					icon-color="#4058D1" :border-bottom="true">
					<u-button size="mini" slot="right" :custom-style="customStyle" :ripple="true"
						ripple-bg-color="#4058D180" @click="getCode">{{codeText}}</u-button>
				</u-field>
				<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
			</view>
			<view class="w1 u-flex u-row-right">
				<text class=" u-margin-right-85 color p u-font-sm" @click="fn_swith">{{swith_login}}</text>

			</view>


			<view class="login_btn w1 u-flex u-flex-col ">
				<view class="w9">
					<u-button :custom-style="customStyle" :ripple="true" ripple-bg-color="#408dd166" shape="circle"
						@click="fn_login">登录</u-button>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customStyle: {
					color: '#ffffff',
					backgroundColor: '#4058D1'
				},
				phone: '',
				pwd: '',
				code: '',
				codeText: '',
				swith_flag: true,
				swith_login: '短信登录'
			}
		},
		methods: {
			//短信登录按钮切换
			fn_swith() {
				this.swith_flag = !this.swith_flag
				if (this.swith_flag) {
					this.swith_login = "短信登录"
				} else {
					this.swith_login = "手机号登录"
				}
			},
			//验证码的秒数
			codeChange(text) {
				this.codeText = text;
			},
			//验证码的处理
			getCode() {
				if (this.phone == "") {
					this.$refs.uToast.show({
						title: '手机号为空',
						type: 'error'
					})
				} else if (!this.$u.test.mobile(this.phone)) {
					this.$refs.uToast.show({
						title: '手机号格式错误',
						type: 'warning'
					})
				} else if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					//发生验证码
					this.req.sendCode(this.phone).then(e => {
						if (e.code == 200) {
							uni.hideLoading()
							this.$refs.uToast.show({
								title: '发送成功',
								type: 'success'
							})
							setTimeout(() => {
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							}, 1000);
						} else {
							uni.hideLoading();
							this.$refs.uToast.show({
								title: e.message,
								type: 'error'
							})
						}
					})
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			fn_login() {
				//判断是手机号登录还是验证码登录
				if (this.swith_flag) {
					if (this.phone == "" || this.pwd == "") {
						this.$refs.uToast.show({
							title: '账号或密码为空',
							type: 'error'
						})
					} else {
						//手机号登录
						this.req.loginPhone({
							phone: this.phone,
							pwd: this.pwd
						}).then(e => {
							console.log(e);
							if (e.code == 200) {
								//保存cookie和userid
								uni.setStorageSync('cookie', e.cookie)
								uni.setStorageSync('userid', e.profile.userId + '')
								//登录成功后跳转首页
								this.$refs.uToast.show({
									title: '登录成功',
									type: 'success',
									isTab:true,
									url: '/pages/index/index',
									
								})
							} else if (e.code == 502) {
								this.$refs.uToast.show({
									title: '账号或密码错误',
									type: 'error'
								})
							} else {
								this.$refs.uToast.show({
									title: '系统错误请等待修复',
									type: 'error'
								})
							}
						})
					}
				} else {
					if (this.phone == "" || this.code == "") {
						this.$refs.uToast.show({
							title: '账号或验证码为空',
							type: 'error'
						})
					} else {
						//验证码和手机验证匹配
						this.req.verifyCode({
							phone: this.phone,
							code: this.code
						}).then(e => {
							if (!e.code == 200) {
								this.$refs.uToast.show({
									title: e.message,
									type: 'error'
								})
							} else {
								//匹配正确结果
								this.req.loginCode({
									phone: this.phone,
									code: this.code
								}).then(e => {
									if (e.code == 200) {
										//保存cookie和userid
										uni.setStorageSync('cookie', e.cookie)
										uni.setStorageSync('userid', e.profile.userId + '')
										//登录成功后跳转首页
										this.$refs.uToast.show({
											title: '登录成功',
											type: 'success',
											isTab:true,
											url: '/pages/index/index'
										})
									} else if (e.code == 400) {
										this.$refs.uToast.show({
											title: '账号或密码错误',
											type: 'error'
										})
									} else {
										this.$refs.uToast.show({
											title: '系统错误请等待修复',
											type: 'error'
										})
									}
								})
							}

						})
					}
				}
			}
		}
	}
</script>

<style>
	.login {
		background-color: #FFFFFF;
	}

	.login_head {
		width: 100%;
		height: 40vh;
	}

	.login_head>image {
		width: 236.16rpx;
		height: 151.42rpx;
	}

	.login_btn {
		margin-top: 100rpx;
	}
</style>
